/**
 * 创建对象的安全模式
 * 知识点
 *      1、new操作符的实质就是对当前对象的this进行不断的赋值
 *         那么没有用new操作符实例化一个类，那么会在全局作用域中执行，this的导向也会发生错误
 */
// 实例化类式没有使用new操作符导致window中出现多个全局变量以及方法
var Book = function(title, time, type) {
    this.title = title;
    this.time = time;
    this.type = type;
};

var book = Book('JavaScript', '2014', 'js');

console.log('book', book); // undefined
console.log('window', window.title, window.time, window.type); // JavaScript, 2014, js

// 使用安全模式，避免创建时丢失new操作符
var BookCopy = function(title, time, type) {
    if (this instanceof Book) {
        this.title = title;
        this.time = time;
        this.type = type;
    } else {
        return new Book(title, time, type);
    }
}

var bookCopy = BookCopy('JavaScript', '2014', 'js');

console.log('bookCopy', bookCopy); //Book
console.log('windowCopy', window.title, window.time, window.type); // undefined, undefined, undefined 
